#_*_coding:utf-8_*_
from main_process_0_ import process
from configure_semantic_segment import parser_rules_root

import os
import sys,getopt

def chunk(s, freq_int, SF):
    result = process(s)
    tmps = ""
    for w in result:
        tmps += w[0] + '\t'
    tmps += str(freq_int)
    SF.write(tmps + '\n')
    return result

def train_and_generate_struct_seperate_files(inputfile, structfile, seperatefile, segment_file_path):
    print("...Start segmenting...")
    input_f = open(inputfile, mode='r', encoding='utf-8')
    struct_f = open(structfile, mode='w', encoding='utf-8')
    seperate_f = open(seperatefile, mode='w', encoding='utf-8')
    seg_result_f = open(segment_file_path, mode='w', encoding='utf-8')
    count = 0
    PW = input_f.readline()
    while PW:
        PW, freq_str = PW.strip('\n').split('\t')
        frequency = int(freq_str)
        result = chunk(PW, frequency, seperate_f)
        seg_result_f.write('%s\t%d\n' % (str(result), frequency))
        res = ''
        for w in result:
            res += w[1]+'\t'
        struct_f.write(res + freq_str + '\n')
        PW = input_f.readline()

        count += 1
        if count % 1000 == 0:
            print("%d passwords done." % count)

    input_f.close()
    struct_f.close()
    seperate_f.close()
    print("...End segmenting")


#
#   input:  1. ifile:   whole path of training file
#           2. ruleset: name of rule set, which will be used as directory name
#
#   example:    train.py --ifile ./xxx.txt --ruleset xxx
#
def main(argv):
    input_file_path = ''
    session_name = ''
    file_path_need_seperated = ''
    generated_segment_result_file_path = ''
    try:
        opts, args = getopt.getopt(argv, 'h', ['training_set=', 'session_name='])
    except getopt.GetoptError:
        print('train.py --training_set <training_set> --session_name <session_name>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print('train.py --training_set <training_set> --session_name <session_name>')
            sys.exit()
        elif opt in ('--training_set'):
            input_file_path = arg
        elif opt in ('--session_name'):
            session_name = arg

    if session_name == '':
        print('session_name missing.')
        exit(1)
    if input_file_path == '':
        print('training_set missing.')
        exit(1)

    rule_set_path = os.path.join(parser_rules_root, session_name, 'Rules')
    struct_file_path = os.path.join(parser_rules_root, session_name, '%s_struct' % session_name)
    seperate_file_path = os.path.join(parser_rules_root, session_name, '%s_seperate' % session_name)
    segment_file_path = os.path.join(parser_rules_root, session_name, '%s_segment_result.txt' % session_name)

    if not os.path.exists(os.path.join(parser_rules_root, session_name)):
        os.mkdir(os.path.join(parser_rules_root, session_name))
    if not os.path.exists(rule_set_path):
        os.mkdir(rule_set_path)

    train_and_generate_struct_seperate_files(input_file_path, struct_file_path, seperate_file_path, segment_file_path)

if __name__ == '__main__' :
    main(sys.argv[1:])
    #target = "[('password', 'NOUN'), ('1', 'Suf1')]"
    #print(load_seg_result(target))
    #result_file_path = r'D:\PythonProjects\SemanticPasswordSegmentTool\Results\gmail_100w_20210716\gmail_100w_20210716_segment_result.txt'
    #rule_set_path = r'D:\PythonProjects\SemanticPasswordSegmentTool\Results\gmail_100w_20210716\Rules'
    #os.makedirs(rule_set_path)
    #generate_rule_files(result_file_path, rule_set_path)

